package org.sql.flow.config;

import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.apache.ibatis.session.TransactionIsolationLevel;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.TimeZone;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;


public class MapperFactory {
    /**
     * 数据库连接
     */
    private SqlSession sqlSession;
    /**
     * 执行
     */
    private static Executor executor;
    /**
     * 配置
     */
    private static Configuration configuration;

    //@Bean(value = "beanSqlSession")
    public  void createSqlSession () {
        try {
            //解析mapper文件
                String userDir = System.getProperty("user.dir");
                Path path = Paths.get(userDir+"/src/main/resources/mapper/");
            File[] fileList = path.toFile().listFiles();
            for (int i=0;i<fileList.length;i++){
                if(Files.exists(path)){
                        Resource rdbMapperResource = new ClassPathResource("mapper/TestMapper.xml");
                        XMLMapperBuilder rdbXmlMapperBuilder = new XMLMapperBuilder(rdbMapperResource.getInputStream(), configuration,
                                rdbMapperResource.toString(), configuration.getSqlFragments());
                        rdbXmlMapperBuilder.parse();
                    }

                }
//            Resource mapperResource = new ClassPathResource(common);
//            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperResource.getInputStream(), configuration, mapperResource.toString(), configuration.getSqlFragments());
//            xmlMapperBuilder.parse();

            //直接实例化一个默认的sqlSession
            //是做单元测试，那么没必要通过SqlSessionFactoryBuilder构造SqlSessionFactory,再来获取SqlSession
            sqlSession = new DefaultSqlSession(configuration, executor, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static {
        try {
            TimeZone newTimeZone = TimeZone.getTimeZone("Asia/Chongqi");
            TimeZone.setDefault(newTimeZone);
            //定义一个配置
            configuration = new Configuration();
            configuration.setCacheEnabled(false);
            configuration.setLazyLoadingEnabled(false);
            configuration.setAggressiveLazyLoading(true);
            configuration.setDefaultStatementTimeout(20);
            //读取测试环境数据库配置
            UnpooledDataSource dataSource = new UnpooledDataSource();
            dataSource.setDriver("");
            dataSource.setUrl("");
            dataSource.setUsername("");
            dataSource.setPassword("");
            //设置事务(测试设置事务不提交false)
            Transaction transaction = new JdbcTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, true);
            //设置执行
            executor = configuration.newExecutor(transaction);
            configuration.setDatabaseId("mysql");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        MapperFactory a=    new MapperFactory();
        a.createSqlSession();
    }


}
